//公共的js
api ={
    //获取当前时间
    GetDate:function () {
        var date=new Date()
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h=h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        minute = minute < 10 ? ('0' + minute) : minute;
        var second=date.getSeconds();
        second=second < 10 ? ('0' + second) : second;
        return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
    },
    test:function () {
        console.log("hello,word");
    },
    GET: function(url, data, callback) {
        axios({
            method: 'get',
            url: url,
            params: data,
            headers: {
                'Authorization': localStorage.getItem('token'),
               // 'Redirect-Header': false,
                'Content-Type': 'application/json'
            }
        }).then(function(response) {
            if (response.status == 200) {
                callback(response.data)
            } else {
                errorTip(response.data.message)
            }
        }).catch(function(error) {
            console.log(error)
        })
    },
    POST: function(url, data, callback) {
        axios({
            method: 'post',
            url: url,
            data: JSON.stringify(data),
            headers: {
                'Authorization': localStorage.getItem('token'),
             //   'Redirect-Header': false,
                'Content-Type': 'application/json',
            }
        }).then(function(response) {
            if (response.status == 200) {
                callback(response.data)
            } else {
                errorTip(response.data.message)
            }
        }).catch(function(error) {
            console.log(error)
        })
    },
    POSTData: function(url, data, callback) {
        axios({
            method: 'post',
            url: url,
            data: data,
            headers: {
                'Authorization': localStorage.getItem('token'),
              //  'Redirect-Header': false
            }
        }).then(function(response) {
            if (response.status == 200) {
                callback(response.data)
            } else {
                errorTip(response.data.message)
            }
        }).catch(function(error) {
            console.log(error)
        })
    },
    PUT: function(url, data, callback) {
        axios({
            method: 'put',
            url: url,
            data: data,
            headers: {
                'Authorization': localStorage.getItem('token'),
              //  'Redirect-Header': false,
            }
        }).then(function(response) {
            if (response.status == 200) {
                callback(response.data)
            } else {
                errorTip(response.data.message)
            }
        }).catch(function(error) {
            console.log(error)
        })
    },
    DELETE: function(url, data, callback) {
        axios({
            method: 'post',
            url: base+url+'/delete',
            data: JSON.stringify(data),
            headers: {
                'Authorization': localStorage.getItem('token'),
               // 'Redirect-Header': false,
                'Content-Type': 'application/json'
            }
        }).then(function(response) {
            if (response.status == 200) {
                callback(response.data)
            } else {
                errorTip(response.data.message)
            }
        }).catch(function(error) {
            console.log(error)
        })
    },
    URLAjax :function (requestObject,url,data) {
            var data1='';
            $.ajax({
                type: requestObject,
                url: url,
                data:data,
                headers: {
                    'Authorization':localStorage.getItem('token')
                },
                async:false,
                success: function(data) {
                    data1=data;
                }
            })
            return data1
    },
    //base64加密
    base64Encode:function(str){
        return btoa(str);
    },
    //base64解密
    base64Decode:function(str) {
        return atob(str);
    },
    //获取cookie
    getCookie:function(name) {
        var cookieArr = document.cookie.split(";"); // 将cookie字符串拆分成数组
        for (var i = 0; i < cookieArr.length; i++) {
            var cookiePair = cookieArr[i].split("="); // 将每个cookie拆分成名称和值的键值对
            var cookieName = cookiePair[0].trim(); // 获取cookie的名称，去除前后空格
            if (cookieName === name) {
                var cookieValue = cookiePair[1]; // 获取cookie的值
                return cookieValue;
            }
        }
        return null; // 如果未找到指定的cookie，则返回null
    },
    //清空cookie
    clearCookie:function (name) {
        document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/";
    },
    //得到uuid
    getuuid:function () {
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
            var r = Math.random() * 16 | 0,
                v = c === 'x' ? r : (r & 0x3 | 0x8);
            return v.toString(16);
        });
    },
    //获取随机的6个字母
    generateRandomLetters:function() {
        var letters = 'abcdefghijklmnopqrstuvwxyz';
        var randomLetters = '';

        for (var i = 0; i < 6; i++) {
            var randomIndex = Math.floor(Math.random() * letters.length);
            randomLetters += letters.charAt(randomIndex);
        }

        return randomLetters;
    }
}
